//
//  CommentUtil.swift
//  SSComment
//
//  Created by 松松 on 2019/5/16.
//

import Foundation

//MARK:Debug打印
func ssPrint(_ items: Any...){
    #if DEBUG
    items.forEach { (item) in
        print(item)
    }
    //    DebugWindow.defauled()?.logData.append(items.description)
    #endif
}

func ssDebug(function:String=#function,_ items: Any...){
    #if DEBUG
    print("\(function):")
    items.forEach { (item) in
        print(item)
    }
    #endif
}


func ssFunc(_ block:()->Void){
    #if DEBUG
    block()
    #endif
}


extension Date{
    //MARK:时间转啥时候以前
    func timeAgoSinceDate() -> String {
        let now = Date()
        let earliest = (now as NSDate).earlierDate(self)
        let latest = (earliest == now) ? self : now

        let time = Int(latest.timeIntervalSince(earliest))

        if time >= 86400{
            let dateFormat = DateFormatter()
            dateFormat.dateFormat = "MM-dd HH:mm"
            dateFormat.locale = Locale(identifier: "zh_CN")
            return dateFormat.string(from: self)
        }else if time > 3600{
            let oldDay: Int = (Calendar.current as NSCalendar).components([NSCalendar.Unit.day], from: earliest).day!
            let nowDay: Int = (Calendar.current as NSCalendar).components([NSCalendar.Unit.day], from: latest).day!
            let dateFormat = DateFormatter()

            if oldDay == nowDay{
                dateFormat.dateFormat = "HH:mm"
            }else{
                dateFormat.dateFormat = "MM-dd HH:mm"
            }

            dateFormat.locale = Locale(identifier: "zh_CN")
            return dateFormat.string(from: self)
        }else if time >= 60{
            return "\(time/60)分钟前"
        }else {
            return "刚刚"
        }

    }
}
